var ym = {};
/**
 * ios回退加载
 * @param event
 */
window.onpageshow = function(event) {
	if (event.persisted) {
	window.location.reload();
	}
	};

ym.perSex=0;
ym.schoolId="a";
ym.slfToken="slfToken";
/**
 * 用户对象
 */
ym.user = {};

ym.defau = {};

ym.childList={};
/**
 * 判断是否为空
 */
ym.isBlank = function(value) {
	if (value == undefined) {
		return true;
	}
	if (value == null) {
		return true;
	}
	if (value == "") {
		return true;
	}
	return false;
}

/**
 * 重写ajax 请求
 */
ym.ajax = function() {
	$("#my-modal-loading").modal('open');
	var operate = arguments[0] || {};
	var url =operate.url || "";
	var async = typeof (operate.async) == 'boolean' ? operate.async : true;
	var param = operate.param || {};
	var dataType = operate.dataType || "jsonp";
	var type = operate.type || "GET";
	var contentType = operate.contentType || "application/x-www-form-urlencoded; charset=utf-8";
	var loading = operate.loading || '正在处理中...';
	var success = operate.success || function(data) {};
	var beforeSend = operate.beforeSend || function(data) {};
	var complete = operate.complete || function(data) {};
	var error = operate.error || function(data) {};
	if (!ym.isBlank(url)) {
		url="http://111.230.17.122:8083"+url;
		//url="http://192.168.0.113:80/wachat"+url;
		$.ajax({
			type : type,
			url : url,
			contentType : contentType,
			// dataType : dataType,
			data :param,
			beforeSend : function(data){
				beforeSend(data);
			},
			error : function(data){
				error(data);
			},
			complete : function(data) {
				complete(data);
			},
			success : function(data) {
				var url = window.location.href;
				var index = url .lastIndexOf("\/");  
				url  = url .substring(index + 1, url .length);
				if(url=="index.html"||url=="course_pack.html"){
					setTimeout(function(){
					  $('#my-modal-loading').modal('close');
					},3000);
				}else{
					$("#my-modal-loading").modal('close');
				}
				if(data){
					if(data.res == "234"){
						alert(data.resMsg);
						window.location.href = "/ym/login.html"
					}else{
						success(data);
					}
				}
			}
		});
	}
}
  // (function ($) {
  //       var _ajax = $.ajax;
  //       //重写jquery的ajax方法  
  //       $.ajax = function (opt) {

  //           //备份opt中error和success方法  
  //           var fn = {
  //               error: function (XMLHttpRequest, textStatus, errorThrown) { },
  //               success: function (data, textStatus) { }
  //           }
  //           if (opt.error) {
  //               fn.error = opt.error;
  //           }
  //           if (opt.success) {
  //               fn.success = opt.success;
  //           }
  //           opt.url = window.AppInfo.baseURL + opt.url;
  //           opt.contentType = (typeof (opt.contentType) == 'undefined' ? "application/x-www-form-urlencoded" : opt.contentType);
  //           opt.dataType = "JSON";
  //           //扩展增强处理  
  //           var _opt = $.extend(opt, {
  //               error: function (XMLHttpRequest, textStatus, errorThrown) {
  //                   mui.toast('连接超时，请稍后重试！');
  //                   //错误方法增强处理  
  //                   fn.error(XMLHttpRequest, textStatus, errorThrown);
  //               },
  //               beforeSend: function (XHR) {
  //                   mui.showLoading(null);
  //                   //设置请求头文件
  //                   var _auth = getAuth();
  //                   XHR.setRequestHeader("Agent-AppAuth", _auth);
  //                   // XHR.setRequestHeader("Access-Control-Allow-Origin","http://192.168.0.123:80")
  //                   //XHR.setRequestHeader("Content-Type", 'application/x-www-form-urlencoded');
  //               },
  //               success: function (data, textStatus) {
  //                   if (data == null) {
  //                       mui.toast('连接超时，请稍后重试！');
  //                       return;
  //                   }
  //                   //成功回调方法增强处理  
  //                   fn.success(data, textStatus);
  //               },
  //               complete: function (XHR, TS) {
  //                   mui.hideLoading(null);
  //               }
  //           });
  //           return _ajax(_opt);
  //       };
  //   })($);

/**
 * 获取用户信息
 */
ym.getUser = function(){
	ym.ajax({
		url : "/user/getUer",
		async : false,
		success : function(data){
			ym.user = data.obj;
		}
	});
}
/**
 * 获取所有孩子信息
 */
ym.getChildList = function (){
    ym.ajax({
        url:'/myCenter/getUserChildrenCourse',
        type:'post',
        async : false,
        param:JSON.stringify({type: "0"}),
        contentType : 'application/json;charset=utf-8',
        success:function(data){
        	console.log(data)
           if(data.res==1){
           	ym.childList=data.obj;
		   }
        }
    });
}

ym.getSequenceChild=function (){
    ym.getChildList();
    for(var a=0,b=ym.childList.length;a<b;a++){
        if(ym.childList[a].sequence==0){
        	ym.perSex=ym.childList[a].sex;
        	ym.schoolId=ym.childList[a].schoolId;
        	return ym.childList[a]
            //return ym.childList[a].childrenId;
        }
    }
}
//是否登录判断
ym.checkSign=function (){
    var loginSign=$.cookie('slfCookie');
    if(loginSign!=undefined){
    	return true
    }else{
    	return false
    }
}

//是否收藏
//type代表：1.文章2.交流圈贴子3.单独课程视频4.课程包视频5.视频知识
//typeid代表:文章id或者视频id
ym.collection=function(type,typeid){
	console.log("进收藏接口")
	ym.ajax({
		url:'/usercollectid/collection',
		param:{ 
			collectionType:type,
			collectionTypeId :typeid,
		},
		success:function(data){
			if(data.res ==1){
				return true
			}else{false}
		}
	});
}
//点赞
//praiseTypeId代表:1.文章2.对医生的提问评论3.交流圈发帖
//userId代表:点赞用户id
ym.praise=function(praiseTypeId,userId,praiseType){
	ym.ajax({
		url:'/communication/addPraise',
		param:{ 
			praiseTypeId:praiseTypeId,
			userId :userId,
			praiseType:praiseType
		},
		success:function(data){
			var data=JSON.parse(data);
			if(data.res ==1){
				return true
			}else{
				return false
			}
		}
	});
}

/**
 * 获取url参数
 */
ym.getQueryStringByName = function(name) {
    var result = location.search.match(new RegExp("[\?\&]" + name + "=([^\&]+)", "i"));
    if (result == null || result.length < 1) {
        return "";
    }
    return decodeURI(result[1]);
}

ym.weekDay = ["星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]; //星期几
ym.orderState = {"1":"已预约成功","2":"预约失败","3":"预约结束，即将开课"};
ym.AttendanceType = {"1":"正常", "2":"迟到","3":"早退"};